/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.isfd.domain.IsfdCollectRoundHistory;
import com.comac.ins.isfd.domain.bo.IsfdCollectRoundHistoryBo;
import com.comac.ins.isfd.domain.vo.IsfdCollectRoundHistoryVo;
import com.comac.ins.isfd.mapper.IsfdCollectRoundHistoryMapper;
import com.comac.ins.isfd.service.IIsfdCollectRoundHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 机型轮次历史记录-采集Service业务层处理
 *
 * @author hxloongs
 * @date 2025-03-24
 */
@RequiredArgsConstructor
@Service
public class IsfdCollectRoundHistoryServiceImpl implements IIsfdCollectRoundHistoryService {

    @Autowired
    private IsfdCollectRoundHistoryMapper baseMapper;

    /**
     * 查询机型轮次历史记录-采集
     *
     * @param id 主键
     * @return 机型轮次历史记录-采集
     */
    @Override
    public IsfdCollectRoundHistoryVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询符合条件的机型轮次历史记录-采集列表
     *
     * @param bo 查询条件
     * @return 机型轮次历史记录-采集列表
     */
    @Override
    public List<IsfdCollectRoundHistoryVo> queryList(IsfdCollectRoundHistoryBo bo) {
        LambdaQueryWrapper<IsfdCollectRoundHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<String> roundHistoryList(String modelNumber) {
        return baseMapper.selectRoundNumbersByModelNumber(modelNumber);
    }

    @Override
    @Transactional
    public void save(String modelNumber, String roundNumber) {
        IsfdCollectRoundHistoryBo bo = new IsfdCollectRoundHistoryBo();
        bo.setModelNumber(modelNumber);
        bo.setRoundNumber(roundNumber);
        List<IsfdCollectRoundHistoryVo> roundHistoryVoList = this.queryList(bo);
        if (!CollectionUtils.isEmpty(roundHistoryVoList)) {
            return;
        }
        IsfdCollectRoundHistory po = new IsfdCollectRoundHistory();
        po.setModelNumber(modelNumber);
        po.setRoundNumber(roundNumber);
        po.setRoundName(roundNumber);
        baseMapper.insert(po);
    }

    private LambdaQueryWrapper<IsfdCollectRoundHistory> buildQueryWrapper(IsfdCollectRoundHistoryBo bo) {
        LambdaQueryWrapper<IsfdCollectRoundHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getModelNumber()), IsfdCollectRoundHistory::getModelNumber, bo.getModelNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getRoundNumber()), IsfdCollectRoundHistory::getRoundNumber, bo.getRoundNumber());
        return lqw;
    }
}
